//
//  WifiInfoViewModel.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/5/19.
//

import Foundation
import Combine
import ThingSmartDeviceKit

class WifiInfoViewModel: ObservableObject {
    @Published var isPasswordHidden = true
    @Published var password: String = ""
    @Published var currentWifiSSID: String = ""
    @Published var showToast: Bool = false
    @Published var toastMsg: String = ""
    
    init()
    {
        self.currentWifiSSID = PermissionManager.shared.getWiFiSSID() ?? ""
        if let info = WIFIStoreTools.shared.getWifiInfo(ssid: self.currentWifiSSID) {
            self.currentWifiSSID = info.ssid
            self.password = info.password
            return
        }
    }
    
    func refreshCurrentWifiSSID()
    {
        self.currentWifiSSID = PermissionManager.shared.getWiFiSSID() ?? ""
    }
    
    func saveWifiInfo() -> Bool
    {
        if(self.currentWifiSSID.isEmpty || self.password.isEmpty)
        {
            showToast = true
            toastMsg = "wifi名称与密码不能为空"
            return false
        }
        // TODO: 可能存在密码错误等情况连接不上，所以此时不可以存储
//        let wifiInfo = WIFIStoreTools.WifiInfo.init(ssid: self.currentWifiSSID, password: self.password)
//        let _ = WIFIStoreTools.shared.saveWifiInfo(wifiInfo)
        CiotManager.shared.wifiSSID = self.currentWifiSSID
        CiotManager.shared.wifiPass = self.password
        return true
    }
    
    
}
